package cn.ehanghai.iot.back.controller;

import cn.ehanghai.iot.back.domain.SysUser;
import cn.ehanghai.iot.back.service.SysUserService;
import cn.ehanghai.iot.common.controller.BaseController;
import cn.ehanghai.iot.common.security.constants.Constants;
import cn.ehanghai.iot.common.security.constants.ReturnCode;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @Author: xumf
 * @Date: 2019/6/26 16:35
 */
@RequestMapping("system")
@RestController
@Slf4j
public class SysUserController extends BaseController {

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private RedisTemplate<String,String> template;

    /**
     * 系统登录
     * @param userName
     * @param password
     * @return
     */
    @RequestMapping(value = "login",method = RequestMethod.POST)
    public ReturnCode login(@RequestBody Map<String, String> map, HttpServletRequest request){
        String userName=map.get("userName");
        String password=map.get("password");
        SysUser sysUser=sysUserService.selectSysUserByUserName(userName);
        log.error("用户信息："+sysUser);
        if(sysUser==null){
            return errorException(Constants.ERROR_CODE_MEMBER_ERROR);
        }else if(!sysUser.getPassword().equals(password)){
            return errorException(Constants.ERROR_CODE_PASSWORD_ERROR);
        }
        request.getSession().setAttribute("sysUser",sysUser);
        return success();

    }
}
